37 Lecture
CS201
Midterm & Final Term Short Notes
Overloading Insertion and Extraction Operators
In C++, the insertion (<<) and extraction (>>) operators can be overloaded to enable custom input and output of user-defined types. Overloading these operators allows objects of a user-defined type to be input or output using the same syntax as
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- Which operator is used for input in C++? a) << b) >> c) & d) *
Answer: b) >>
- Which operator is used for output in C++? a) + b) * c) >> d) <<
Answer: d) <<
- Which function is used to overload the extraction operator? a) operator- b) operator>> c) operator< d) operator[]
Answer: b) operator>>
- Which function is used to overload the insertion operator? a) operator< b) operator>> c) operator- d) operator<<
Answer: d) operator<<
- What is the return type of the overloaded insertion operator? a) void b) int c) ostream& d) istream&
Answer: c) ostream&
- What is the return type of the overloaded extraction operator? a) void b) int c) ostream& d) istream&
Answer: d) istream&
- What is the first parameter of the overloaded insertion operator? a) ostream& b) istream& c) int d) char
Answer: a) ostream&
- What is the first parameter of the overloaded extraction operator? a) ostream& b) istream& c) int d) char
Answer: b) istream&
- What is the second parameter of the overloaded insertion operator? a) ostream& b) istream& c) int d) const Object&
Answer: d) const Object&
- What is the second parameter of the overloaded extraction operator? a) ostream& b) istream& c) int d) Object&
Answer: d) Object&
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the purpose of overloading insertion and extraction operators? Answer: Overloading these operators enables custom input and output of user-defined types.
How do you overload the insertion operator in C++? Answer: The insertion operator is overloaded using the syntax: ostream& operator<<(ostream& os, const Object& obj)
How do you overload the extraction operator in C++? Answer: The extraction operator is overloaded using the syntax: istream& operator>>(istream& is, Object& obj)
What is the return type of the overloaded insertion operator? Answer: The return type of the overloaded insertion operator is ostream&.
What is the return type of the overloaded extraction operator? Answer: The return type of the overloaded extraction operator is istream&.
How do you define an insertion operator for a class in C++? Answer: You define an insertion operator for a class in C++ by declaring a friend function of the class.
How do you define an extraction operator for a class in C++? Answer: You define an extraction operator for a class in C++ by declaring a friend function of the class.
Can you overload the insertion and extraction operators for built-in types in C++? Answer: No, these operators cannot be overloaded for built-in types in C++.
What is the purpose of the 'const' keyword in the insertion operator's parameter list? Answer: The 'const' keyword in the insertion operator's parameter list specifies that the object being inserted should not be modified.
What is the purpose of the '&' symbol in the insertion and extraction operator's parameter list? Answer: The '&' symbol in the insertion and extraction operator's parameter list specifies that the parameters should be passed by reference.